<!--
 * @Author: Sam
 * @Date: 2020-11-02 10:35:37
 * @LastEditTime: 2022-01-17 17:18:37
 * @LastEditors: Sam
 * @Description:
 * @FilePath: \resourcesf:\thework\ygboost-eui\src\components\app\edit\setting\channel\channelHistoricalPriceList.vue
 * @可以输入预定的版权声明、个性签名、空行等
-->
<template>
  <div class="padding">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-card class="text-left">
          <div slot="header" class="clearfix">
            <span class="title-icon">历史价格表</span>
          </div>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="count"
          >
          </el-pagination>
          <el-table
            size="mini"
            :data="tabsList"
            :border="true"
            @selection-change="handleSelectionChange"
          >
            <!--<el-table-column type="selection" align="center" />-->
            <el-table-column
              prop="fileName"
              label="文件名"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="effectiveTime"
              label="生效时间"
              show-overflow-tooltip
            >
            </el-table-column>

            <el-table-column prop="cuser" label="创建人">
              <template slot-scope="scope">
                {{ cuserMap[scope.row.cuser] }}
              </template>
            </el-table-column>
            <el-table-column prop="cdate" label="创建时间"> </el-table-column>
            <el-table-column prop="fileUrl" label="操作" show-overflow-tooltip>
              <template slot-scope="scope">
                <!-- {{fileUrl}}
                {{scope.row.fileUrl}} -->
                <el-link
                  type="primary"
                  :href="`${fileUrl}${downloadPath}/${scope.row.fileUrl}`"
                  >点击下载</el-link
                >
                <el-tooltip content="预览">
                  <i
                    style="color: green"
                    class="el-icon-view text-xl margin-right-sm"
                    @click="previewFile(scope.$index, scope.row)"
                  ></i>
                </el-tooltip>
                <template v-if="scope.row.del == true">
                  <span>已删除</span>
                </template>
                <template v-else>
                  <el-link
                    v-if="scope.row.effectiveTime > currentTime"
                    @click="deleteChannelHistoryPriceFile(`${scope.row.id}`)"
                    style="color: grey; margin-left: 10px"
                    >删除</el-link
                  >
                </template>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>

      <el-col :span="12">
        <el-card class="text-left">
          <div slot="header" class="clearfix">
            <span class="title-icon">重新生成价格</span>
          </div>
          <template v-if="type == 2">
            <el-tag>重新生成费用请点击下列链接前往</el-tag>
            <p>
              <el-link
                type="primary"
                :href="`/#/blank/amoeba__amoebaAirChannel__main`"
                >阿米巴空运渠道管理</el-link
              >
            </p>
            <p>
              <el-link
                type="primary"
                :href="`/#/blank/amoeba__amoebaAirChannel__main`"
                >阿米巴海运渠道管理</el-link
              >
            </p>
            <p>
              <el-link
                type="primary"
                :href="`/#/blank/amoeba__amoebaAirChannel__main`"
                >阿米巴快递渠道管理</el-link
              >
            </p>
          </template>
          <template v-else>
            <el-row :gutter="20">
              <el-col :span="10">
                <el-input
                  type="textarea"
                  :rows="20"
                  placeholder="运单号,多个运单(最多300个)请用逗号、换行隔开,修改结果会显示在右边"
                  v-model="waybillNos"
                >
                </el-input>
                <div class="margin-tb">
                  <el-button
                    size="mini"
                    type="primary"
                    @click="batchReBuildChannelCost"
                    >确定</el-button
                  >
                </div>
              </el-col>
              <el-col :span="14">
                <p>最后一次任务创建时间：{{ channelPriceTask.cdate }}</p>
                <p style="margin-top: 10px">
                  创建人：{{ channelPriceTask.userName }}
                </p>
                <p style="margin-top: 10px">状态：{{ status }}</p>
                <p style="margin-top: 10px">成功运单：{{ successNos }}</p>
                <p style="margin-top: 10px">失败运单：{{ failNos }}</p>
                <p style="margin-top: 10px">运行中运单：{{ excuteNos }}</p>
              </el-col>
            </el-row>
          </template>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { ajax } from "@/store/yg/common/utils";
import TableComponent from "./TableComponent";
import { checkRowsEmpty, parseTime } from "@/utils";
import * as constant from "@/store/yg/common/constant";
import ViewInfo from "../../../main/waybill/view/viewInfo";
export default {
  components: {
    ViewInfo,
    TableComponent,
  },
  data() {
    return {
      fileUrl: constant.FILE_SERVER,
      cuserMap: {},
      tabsList: [],
      currentPage: 1,
      count: 1,
      pageSize: 10,
      currentTime: parseTime(new Date().toLocaleDateString()),
      type: null,
      waybillNos: null,
      channelPriceTask: { cdate: null, userName: null },
      status: null,
      excuteNos: null,
      successNos: null,
      failNos: null,
      downloadPath: null,
    };
  },
  mounted() {
    this.init();
    let path = this.$route.path;
    if (path === "/main/setting__customerchannel__list") {
      this.type = 0;
      this.downloadPath = "customerchannel";
    } else if (path === "/main/setting__servicerchannel__list") {
      this.type = 1;
      this.downloadPath = "servicerchannel";
    } else if (path === "/main/setting__amoebacustomerchannel__list") {
      this.type = 2;
      this.downloadPath = "amoebaChannel";
    }
  },

  methods: {
    async init() {
      // const uri = "/api/entry/source?bean=userSource";
      // const { data: userOptions } = await axios.post(constant.API_SERVER + uri);
      const attrs = {
        class: "com.tofba.app.client.seaair.CustomerChannelClient",
      };
      const { userMap } = await ajax("initCustomerChannelData", {}, attrs);
      this.cuserMap = userMap;

      const tabsList = await ajax(
        "queryChannelHistoryPriceFiles",
        {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          fileType: this.type,
        },
        attrs
      );

      console.log("tabsList", tabsList);

      if (tabsList.code === 200) {
        this.tabsList = tabsList.datas;
        this.count = tabsList.count;
        if (tabsList.channelPriceTask) {
          this.channelPriceTask = tabsList.channelPriceTask;
        }
        this.status = tabsList.status;
        this.excuteNos = tabsList.excuteNos;
        this.successNos = tabsList.successNos;
        this.failNos = tabsList.failNos;
      } else {
        this.$message.error(tabsList.message);
      }
    },
    shippingWayClick(tab) {
      this.init();
    },
    previewFile(index, row) {
      console.log(row);
      var param = {};

      var fileName = row.fileUrl;
      param["fileName"] = fileName;
      var name = row.fileName;
      var uploadpath = "/";
      console.log(fileName);
      var path = (path = constant.FILE_SERVER + uploadpath);
      var fileUrl = path + fileName;
      // var url = "/#/view/viewWaybillFile?fileUrl=" + fileUrl;
      var fileExt = /[.]/.exec(fileName)
        ? /[^.]+$/.exec(fileName.toLowerCase())
        : ""; // 文件扩展名
      console.log("names", fileExt);
      if (
        fileExt == "pdf" ||
        fileExt == "jpg" ||
        fileExt == "tif" ||
        fileExt == "bmp" ||
        fileExt == "gif" ||
        fileExt == "png" ||
        fileExt == "jpeg"
      ) {
        window.open(fileUrl, "_blank");
      } else if (
        fileExt == "doc" ||
        fileExt == "docx" ||
        fileExt == "xlsx" ||
        fileExt == "xls" ||
        fileExt == "pptx" ||
        fileExt == "ppt"
      ) {
        const attrs = {
          class: "com.tofba.app.client.seaair.CustomerChannelClient",
        };
        ajax("filePreviewByServer", param, attrs).then(
          (res) => {
            if (res.code != 200) {
            } else {
              window.open(res.data);
            }
          },
          (error) => {
            console.log(error);
          }
        );
      }
    },
    async deleteChannelHistoryPriceFile(id) {
      console.log(id);
      const attrs = {
        class: "com.tofba.app.client.seaair.CustomerChannelClient",
      };
      const response = await ajax(
        "deleteChannelHistoryPriceFile",
        { id: id },
        attrs
      );
      if (response.code === 200) {
        this.$message.success("删除成功");
        await this.init();
      } else {
        this.$message.error(response.message);
      }
    },
    async batchReBuildChannelCost() {
      const attrs = {
        class: "com.tofba.app.client.seaair.CustomerChannelClient",
      };
      const response = await ajax(
        "batchCreateChannelCost",
        { waybillNos: this.waybillNos, channelType: this.type },
        attrs
      );
      if (response.code === 200) {
        this.$message.success("任务已经创建");
        this.waybillNos = null;
        await this.init();
      } else {
        this.$message.error(response.message);
      }
    },
    handleSelectionChange(val) {
      console.log(
        "%cval: ",
        "color: MidnightBlue; background: Aquamarine;",
        val
      );
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.init();
    },
  },
};
</script>
<style></style>
